package com.example.service;

import com.example.entity.Order;
import com.example.entity.Order_Product;
import com.example.mapper.OrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Resource
    private OrderMapper orderMapper;


    public void createOrder(Order order) {
        Date date = new Date();
        order.setCreateTime(LocalDateTime.now());
        orderMapper.addOrder(order);
    }

    public PageInfo<Order_Product> selectPageByUser(Integer currentPage, Integer pageSize, Integer id) {
        PageHelper.startPage(currentPage, pageSize);
        List<Order_Product> list = orderMapper.selectPageByUser(id);
        return PageInfo.of(list);
    }

    public void updateStatus(Order order) {
        orderMapper.updateOrder(order);
    }

    public void updatePayTime(Order order) {
        order.setPaymentTime(LocalDateTime.now());
        orderMapper.updateOrder(order);
    }

    public void updateEndTime(Order order) {
        order.setEndTime(LocalDateTime.now());
        orderMapper.updateOrder(order);
    }

    public Order selectByNo(String orderNo) {
        return orderMapper.selectByNo(orderNo);
    }

    public PageInfo<Order_Product> selectPageByBusiness(Integer currentPage, Integer pageSize, Integer id) {
        PageHelper.startPage(currentPage, pageSize);
        List<Order_Product> list = orderMapper.selectPageByBusiness(id);
        return PageInfo.of(list);
    }

    public PageInfo<Order_Product> selectPageByBusiness_status(Integer currentPage, Integer pageSize, Integer id, Integer status) {
        PageHelper.startPage(currentPage, pageSize);
        List<Order_Product> list = orderMapper.selectPageByBusiness_status(id, status);
        return PageInfo.of(list);
    }

    public PageInfo<Order_Product> selectPageByUser_status(Integer currentPage, Integer pageSize, Integer id, Integer status) {
        PageHelper.startPage(currentPage, pageSize);
        List<Order_Product> list = orderMapper.selectPageByUser_status(id, status);
        return PageInfo.of(list);
    }

}

